# 高可用配置
---> Redis (Replica)
client ---> Redis (Master)
---> Redis (Replica)
# 當Master掛掉時,會把Replica切換為Master提供用戶端服務
Client -----------------------> Redis (Master)
Client | 同步
Redis (X) V
Redis (Replica)
# 關閉Redis Server (Master) 6379
# 確認Redis Server (Replica) 6380 角色
# role:slave
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:2590
master_link_down_since_seconds:327
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:943718468a346457e58ad233607f464998e6159c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2590
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2590
# 確認Redis Server (Replica) 6381 角色
# role:slave
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:2590
master_link_down_since_seconds:355
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:943718468a346457e58ad233607f464998e6159c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2590
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:2576
# 將Redis Server (Replica) 6381 手動轉換為Master
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> replicaof no one
OK
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:943718468a346457e58ad233607f464998e6159c
master_repl_offset:2590
second_repl_offset:2591
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:2576
# 將Redis Server (Replica) 6380 與 6381建立Replication
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> replicaof 127.0.0.1 6381
OK
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:2604
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:943718468a346457e58ad233607f464998e6159c
master_repl_offset:2604
second_repl_offset:2591
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2604
# 確認目前架構 Redis Server (Master) 6381 與 Redis Server (Replica) 6380
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=2632,lag=1
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:943718468a346457e58ad233607f464998e6159c
master_repl_offset:2632
second_repl_offset:2591
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:2618
# 啟動服務 6379
$ ./redis-server redis_6379.conf
# Redis Server (Master) 6379 目前角色為Master
./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:0f0546acb0aecd970497a0dac52e71539d3b2f16
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# 將 Redis Server (Master) 6379 角色轉換為 Replica
127.0.0.1:6379> replicaof 127.0.0.1 6381
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:4018
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4018
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4019
repl_backlog_histlen:0
# 確認目前高可用架構
$ ./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=4046,lag=0
slave1:ip=127.0.0.1,port=6379,state=online,offset=4046,lag=0
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:943718468a346457e58ad233607f464998e6159c
master_repl_offset:4046
second_repl_offset:2591
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:4032
透過人為手動機制雖然可以達成情境所需要的結果,但是實務上還是會有Redis服務操作中斷的問題,故我們希望的高可用架構是可以不中斷服務操作的,此時需要經由第三方sentinel機制來達成自動切換的方式,不需要人為介入就能將某個Replica轉換成為新的Master.
Sentinel功能
sentinel組態設定
cp /home/redis/redis-6.2.5/sentinel.conf sentinel_26379.conf
vi sentinel_26379.conf
# 修改以下項目組態設定
port 26379
daemonize yes
logfile "/tmp/redis-sentinel_26379.log"
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
./redis-sentinel sentinel_26379.conf
# 確認是否已經啟動sentinel
ps -ef|grep redis-s
# 關閉Redis Server (Master) 6379
# 連線至Redis Server (Replica) 6380 確認角色
./redis-cli -h 127.0.0.1 -p 6380
# 確認Redis Server (Replica) 6380 已自動切換Redis Server (Master) 6380
# 當Redis Server (Master) 6379 修復後會轉為Redis Server (Replica) 6379
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=23418,lag=0
slave1:ip=127.0.0.1,port=6379,state=online,offset=23551,lag=0
master_failover_state:no-failover
master_replid:ea3bc11d99568d87b0496fb83c6f2ccc58425baa
master_replid2:a30878092cdd31e404af76eaa7a2d024c8ae843e
master_repl_offset:23551
second_repl_offset:19299
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:71
repl_backlog_histlen:23481